package one.one.thirtyOne;

import java.util.*;

import edu.princeton.cs.algs4.StdDraw;

public class RandomConnect {

	static void randomeConnection(int N, double p) {
		StdDraw.circle(0.5, 0.5, 0.5);
		StdDraw.setPenColor(StdDraw.BLACK);
		StdDraw.setPenRadius(0.05);

		int s = (int) (N * p);
		// System.out.println("s:" + s);
		List<String> l = new ArrayList<String>();
		for (int i = 0; i < N; i++) {
			for (int j = i + 1; j < N; j++) {
				l.add(i + "-" + j);
			}
		}
		String[] a = new String[l.size()];
		a = l.toArray(a);
		System.out.println(Arrays.toString(a));

		Random random = new Random();
		String[] r = new String[s];
		for (int i = 0; i < s; i++) {
			r[i] = a[random.nextInt(l.size())];
		}
		System.out.println(Arrays.toString(r));

		double[][] params = new double[N][2];
		double n = 0;
		for (int i = 0; i < N; i++) {
			n = (Math.PI * 2.0 * i) / N;
			params[i][0] = 0.5 + 0.5 * Math.cos(n);
			params[i][1] = 0.5 + 0.5 * Math.sin(n);
			StdDraw.point(params[i][0], params[i][1]);
		}

		StdDraw.setPenColor(StdDraw.GRAY);
		StdDraw.setPenRadius(0.01);
		String[] idx;
		int pre;
		int post;
		for (int i = 0; i < s; i++) {
			idx = r[i].split("-");
			pre = Integer.parseInt(idx[0]);
			post = Integer.parseInt(idx[1]);
			StdDraw.line(params[pre][0], params[pre][1], params[post][0], params[post][1]);
		}

	}

	public static void main(String[] args) throws Exception {

		Scanner scanner = new Scanner(System.in);
		System.out.print("N(>1):");
		int N = scanner.nextInt();
		System.out.println();
		System.out.print("p(0<p<1):");
		double p = scanner.nextDouble();
		
		scanner.close();

		randomeConnection(N, p);

	}

}
